﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using Microsoft.Ccr.Core;
using XcoAppSpaces.Contracts.CcrWiring;

namespace XcoAppSpaces.Serialization.Binary
{
	class SurrogateSelector : ISurrogateSelector
	{
		private readonly PortSerializationSurrogate portSurrogate;
		private readonly TypeSerializationSurrogate typeSurrogate;
		private readonly Type portType = typeof(IPort);
		//private readonly Type typeType = typeof(Type);

        public SurrogateSelector(ICcrWirer ccrWirer)
        {
            portSurrogate = new PortSerializationSurrogate(ccrWirer);
			typeSurrogate = new TypeSerializationSurrogate();
        }

		public void ChainSelector(ISurrogateSelector selector)
		{
		}

		public ISurrogateSelector GetNextSelector()
		{
			return null;
		}

		public ISerializationSurrogate GetSurrogate(Type type, StreamingContext context, out ISurrogateSelector selector)
		{
			//Console.WriteLine("SurrogateSelector - Type: " + type.FullName + " " + type.BaseType.FullName);
			if ((type == portType || type.GetInterfaces().Contains(portType)))
			{
				selector = this;
				return portSurrogate;
			}
			else if (type.FullName == "System.RuntimeType")
			{
				selector = this;
				return typeSurrogate;
			}
			else
			{
				selector = null;
				return null;
			}
		}
	}
}